package com.jufa_3.controller;

import com.alibaba.fastjson.JSONObject;
import com.jufa_3.common.R;
import com.jufa_3.dto.ChargeRecordVO;
import com.jufa_3.mapper.ChargeRecordMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/cr")
public class ChargeRecordController {

    @Autowired
    private ChargeRecordMapper chargeRecordMapper;

    @GetMapping("/getSelectRecord")
    public R<Object> userSelectRecord(String openId, String keyWord, Integer pageNum) {
        List<ChargeRecordVO> chargeRecordVOs = chargeRecordMapper.getSelectRecord(openId, keyWord, 30 * pageNum, 30);
        if (chargeRecordVOs.size() != 0) {
            for (ChargeRecordVO crvo : chargeRecordVOs) {
                if (crvo.getChargeName().contains("监测")) {
                    crvo.setResult("监测启动成功");
                } else {
                    if (crvo.getInterUseRecord() != null && StringUtils.isNotEmpty(crvo.getInterUseRecord().getQueryResult()) && crvo.getInterUseRecord().getQueryResult().contains("成功")) {
                        crvo.setResult("成功");
                    }
                }
            }
            return R.success(chargeRecordVOs);
        } else {
            return R.success(1005, "没有收费/查询记录");
        }
    }

    @GetMapping("/getChargeRecord")
    public R<Object> getChargeRecord(String mobile, String keyWord, Integer pageNum, Integer pageSize) {
        JSONObject resultJs = new JSONObject();
        resultJs.put("total", chargeRecordMapper.getSelectRecordCount(mobile, keyWord));
        List<Map<String, Object>> selectRecordsReturnMap = chargeRecordMapper.getSelectRecordsReturnMap(mobile, keyWord, pageSize * --pageNum, pageSize);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//定义格式，不显示毫秒
        for (Map map : selectRecordsReturnMap) {

            map.replace("charge_time", df.format(map.get("charge_time")));
        }
        resultJs.put("records", selectRecordsReturnMap);
        return R.success(resultJs);
    }
}
